//
//  WelcomeViewController.m
//  SweetComics
//
//  Created by dlios on 15-3-4.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "WelcomeViewController.h"

@interface WelcomeViewController ()
@property (nonatomic, retain) NewestViewController *newest;
@property (nonatomic, assign) NSInteger flag;
@end

@implementation WelcomeViewController
@synthesize timer;
@synthesize splashImageView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置视图大小
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds ];
    view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    self.view = view;
    [view release];
    // 添加小图标的标记
    _flag = 0;
    
    
    // 添加第一张图片
    splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h1.png"]];
    splashImageView.frame = self.view.bounds;
    [self.view addSubview:splashImageView];

    // 设置定时器弹出第二张图片
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(addLabel2) userInfo:nil repeats:NO];
    
    // 主页
    self.newest = [[NewestViewController alloc] init];
    // 将主页透明度设为0
    self.newest.view.alpha = 0.0;
    [self.view addSubview:self.newest.view];
    [_newest.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_newest release];
    
    // 设置定时器结束动画
    timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
    
}
- (void)buttonAction:(UIButton *)button
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}
- (void)addLabel2{
    // 显示另一张图片
    splashImageView.image = [UIImage imageNamed:@"h2.png"];
    
    
    // 第二张图片出现的动画
    [UIView transitionWithView:splashImageView duration:2.f options: UIViewAnimationOptionTransitionCurlDown  animations:^{
        splashImageView.frame = self.view.bounds;
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil]; // begins animation block
    // 动画存在时间
    [UIView setAnimationDuration:2.0];        // sets animation duration
    [UIView setAnimationDelegate:self];        // sets delegate for this block
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];   // calls the finishedFading method when the animation is done (or done fading out)
    self.view.alpha = 0.0;       // Fades the alpha channel of this view to "0.0" over the animationDuration of "0.75" seconds
    [UIView commitAnimations];   // commits the animation block.  This Block is done.
}


- (void) finishedFading
{
    
    [UIView beginAnimations:nil context:nil]; // begins animation block
    
    
    // 显示主页时间
    [UIView setAnimationDuration:0.75];        // sets animation duration
    self.view.alpha = 1.0;   // fades the view to 1.0 alpha over 0.75 seconds
    self.newest.view.alpha = 1.0;
    [UIView commitAnimations];   // commits the animation block.  This Block is done.
    
    //    for(UIView *mylabelview in [self.view subviews])
    //    {
    //        if ([mylabelview isKindOfClass:[UILabel class]]) {
    //            [mylabelview removeFromSuperview];
    //        }
    //    }
    
    [splashImageView removeFromSuperview];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
